import {
    findInApplyById,
    getInGoodWithInApplyId,
    addNewInApply,
    alterInApply,
    getInGoodWithOutApplyId
} from "../../../api/afftersale/refacin.js";
import {
    findOutApply,
    findOutApplyById,
} from "../../../api/afftersale/refacout";


let inApplyInfo = {
    data() {
        return {
            id: "",
            type: "",//0 修改，1 展示 2添加
            dialogTableVisible: false,//模态框可见性
            innerVisible: false,//内层模态框可见性
            val: '1',//下拉选项默认值

            //申请单信息
            comments: "",
            idoCode: "",
            odoId: "",
            odoCode: "",
            refacLocation: "",
            outApplyCreateBy: "",
            outApplyCreatedTime: "",


            //选择的商品列表
            selectedGoodList: [],

            //审核信息
            approver: "",
            approverTime: "",
            approverComments: "",
            approveStatus: "",
            formatApproveTime: "",

            //出库单搜索
            search: {
                currentPage: 1,
                pageSize: 5,
                odoCode: "",
                createBy: "",
                startTime: "",
                endTime: "",
                status: 1,
            },
            createTime: [],

            //出库单信息
            applyList: [],
            total: "",
            currentPage: 1,
            pageSize: 5,
        }
    },
    methods: {
        //根据id查询信息
        findById(id) {
            let vm = this;
            //查询入库单信息
            findInApplyById(id).then(value => {
                vm.id = value.id;
                vm.comments = value.comments;
                vm.approver = value.approver;
                vm.approveStatus = value.approveStatus;
                vm.approverComments = value.approverComments;
                vm.approverTime = value.approverTime;
                vm.idoCode = value.idoCode;
                vm.odoId = value.odoId;
                vm.formatApproveTime = vm.formatter(value.approverTime);
                //获取对应的出库申请单信息
                findOutApplyById(vm.odoId).then(value1 => {
                    vm.odoCode = value1.odoCode;
                    vm.refacLocation = value1.refacLocation;
                    vm.outApplyCreateBy = value1.createBy;
                    vm.outApplyCreatedTime = vm.formatter(value1.createTime);
                });
                getInGoodWithInApplyId(vm.id).then(value1 => {
                    vm.selectedGoodList = value1
                });
            });
            //查询出库单信息

        },
        getUser() {

        },

        //选择商品
        openAdd() {
            this.dialogTableVisible = true;
        },

        //维修、返厂选项修改
        flagChange(row) {
            if (row.refacType === 0)
                row.newImei = "";
        },

        //保存申请单
        saveNewApply() {
            let vm = this;
            let params = {
                odoId: vm.odoId,
                odoCode: vm.odoCode,
                status: 0,
                createBy: "",
                comments: vm.comments,
                refacLocation:vm.refacLocation,
                refacInGoods: vm.selectedGoodList
            };
            addNewInApply(params).then(value => {
                this.$message.success(value);
                this.returnMan();
            })
        },

        //修改申请单
        alterApply() {
            let vm = this;
            let params = {
                id: vm.id,
                comments: vm.comments,
                refacInGoods: vm.selectedGoodList
            };
            alterInApply(params).then(value => {
                this.$message.success(value);
                this.returnMan();
            })
        },

        formatter(row) {
            if (row != null)
                return row.replace("T", " ");
        },

        //返回管理页面
        returnMan() {
            this.$router.push({path: '/refacinman'});
        },

        //查询出库申请单
        find(params) {
            let vm = this;
            if (params == null) {
                params = {
                    currentPage: vm.$data.currentPage,
                    pageSize: vm.$data.pageSize,
                    status: 1,
                }
            }
            findOutApply(params).then(value => {
                vm.applyList = value.records;
                vm.total = value.total;
            })
        },
        //制单日期
        chooseTime() {
            this.search.startTime = this.createTime[0];
            this.search.endTime = this.createTime[1];
        },
        findPage() {
            this.find(this.search);
        },
        resetForm() {
            this.search = {
                odoCode: "",
                createBy: "",
                startTime: "",
                endTime: "",
            };
            this.find()
        },

        //时间格式化
        creatFormatter(row) {
            if (row.createTime !== null) {
                return row.createTime.replace("T", " ");
            }
        },

        //选择出库单
        choose(id) {
            //获取对应的出库申请单信息
            findOutApplyById(id).then(value1 => {
                this.odoId = value1.id;
                this.odoCode = value1.odoCode;
                this.refacLocation = value1.refacLocation;
                this.outApplyCreateBy = value1.createBy;
                this.outApplyCreatedTime = this.formatter(value1.createTime);
            });
            getInGoodWithOutApplyId(id).then(value1 => {
                this.selectedGoodList = value1
            });
            this.comments = "";
            this.dialogTableVisible = false;
        },

        //打开内层模态框
        showInfo(id) {
            this.innerVisible = true;
            this.$router.push({path: '/son', query: {flag: 4, id: id}});
        }
    },
    created() {
        let vm = this;
        let id = this.$route.query.id;
        let flag = this.$route.query.flag;
        if (id !== undefined) {
            //进行axios，查询对应的id
            vm.id = id;
            vm.findById(id);
        }
        if (flag !== undefined) {
            vm.type = flag;
        }
        this.find();
    }
};

export default inApplyInfo;